﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PSLib.CodeFormating.Lexems;
using PSLib.CodeFormating;

namespace Editor
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class Window1 : Window
	{
		public Window1()
		{
			InitializeComponent();
		}

		private void Window_Loaded(object sender, RoutedEventArgs e)
		{
			LexemList list = LexemParser.Parse(Properties.Resources.TextFile1, PSLib.CodeFormating.Adapters.LanguageAdapter.LuaAdaptor);
			
			// Create a FlowDocument
			FlowDocument mcFlowDoc = new FlowDocument();
			// Create a paragraph with text
			Paragraph para = new Paragraph();

			foreach (var item in list)
			{
				Run run = new Run(item.LexemSource);
				switch (item.Type)
				{
					case LexemType.None:
						break;
					case LexemType.Space:
						break;
					case LexemType.BreakLine:
						break;
					case LexemType.Operator:
						run.Foreground = new SolidColorBrush(Colors.Blue);
						break;
					case LexemType.Symbol:
						break;
					case LexemType.Punctuation:
						break;
					case LexemType.Word:
						break;
					case LexemType.Constant:
						break;
					case LexemType.Digit:
						break;
					case LexemType.String:
						run.Foreground = new SolidColorBrush(Colors.DarkRed);
						break;
					case LexemType.Comment:
						run.Foreground = new SolidColorBrush(Colors.Green);
						break;
					default:
						break;
				}
				para.Inlines.Add(run);
			}
			mcFlowDoc.Blocks.Add(para);
			
			//r.Foreground = new SolidColorBrush(Colors.Red);

			richTextBox1.Document = mcFlowDoc;
		}

		private void CreateParser()
		{
		}

		private void Button_Click(object sender, RoutedEventArgs e)
		{
			FlowDocument doc = richTextBox1.Document;
		}

		private void richTextBox1_TextChanged(object sender, TextChangedEventArgs e)
		{
			var document = richTextBox1.Document;
			foreach (var item in e.Changes)
			{
				TextChange change = item;
				TextPointer pt = document.ContentStart.GetPositionAtOffset(change.Offset);
				Run parent = pt.Parent as Run;
				//richTextBox1.Document.ContentStart.
			}
		}

		private void richTextBox1_SelectionChanged(object sender, RoutedEventArgs e)
		{
			TextSelection s = richTextBox1.Selection;
			Run run = s.Start.Parent as Run;
			textBlock.Text = run.Text;
		}

	}
}
